package com.example.service.impl;

import com.example.dto.UserAddDTO;
import com.example.dto.UserPageQueryDTO;
import com.example.entity.PageBean;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.result.PageResult;
import com.example.service.UserService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 删除用户
     * @param Sno
     */

    @Override
    public void dete(Integer Sno) {
        log.info("删除数据-server");
        userMapper.dele(Sno);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean pageQuery(Integer page, Integer pageSize) {
        Long count = userMapper.count();
       Integer start = (page-1)*pageSize;
        List<User> list = userMapper.pag(start, pageSize);
        PageBean pageBean = new PageBean(count, list);
        return pageBean;
    }

    /**
     * 添加数据
     * @param userDTO
     */
    @Override
    public void add(UserAddDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setState("1");
        user.setCreatedTime(LocalDate.now());
        userMapper.add(user);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User see(Integer id) {
       User user = userMapper.see(id);
        return user;
    }

    @Override
    public void update(UserAddDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        user.setState("1");
        user.setCreatedTime(LocalDate.now());
        userMapper.update(user);
    }

    @Override
    public void conAdd(String conn) {
        userMapper.conAdd(conn);
    }

//    @Override
//    public void adds(User user) {
//        user.setState("1");
//        user.setCreatedTime(LocalDate.now());
//        userMapper.add(user);
//    }

//    @Override
//    public PageBean pageQuery1(UserPageQueryDTO userDTO) {
//        PageHelper.startPage(userDTO.getPage(),userDTO.getPageSize());
//        List<User> list = userMapper.page();
//        Page<User> p =(Page<User>) list;
//        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
//        return pageBean;
//    }


}
